﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FederationMembershipDB
{
    public partial class SelectMember : Form
    {
        Member member = new Member();        
        DataSet ds = new DataSet();
        string searchType;

        public SelectMember(Member _member, DataSet _ds, string _searchType)
        {
            InitializeComponent();
            member = _member; //assign the passed in member object, to the local member object
            ds = _ds;
            searchType = _searchType;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //Continue back to the main form, with the selected member

            //check at least one member has been selected
            if (lsvSelectMember.SelectedIndices.Count == 0)
            {
                string strQ = "Please select a Member, or click 'Cancel' to go back to the main form.";
                string strTitle = "Warning: Selection not made.";
                //Referenece: http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx retrieved: 21/09/2012
                MessageBox.Show(this, strQ, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //get the selected member's ID, first name, and last name. Return this to the main form
                //Selecteditems[0] - the selected item (only one item can selected [this is configurable in the designer > properties for the listview]
                //SubItems[x] - the columns in the list view
                member.ID = int.Parse(lsvSelectMember.SelectedItems[0].SubItems[0].Text); 
                member.GivenName = lsvSelectMember.SelectedItems[0].SubItems[1].Text;
                member.LastName = lsvSelectMember.SelectedItems[0].SubItems[2].Text;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //returns to the Main Form - cancels any selections made
            lsvSelectMember.SelectedIndices.Clear();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void SelectMember_Load(object sender, EventArgs e)
        {
            //when the form loads load the results from the dataset (ds)

            //Table[0] - is the count of how many members with the given name
            //Table[1] - is the count of how many members with the searched for last name
            //Table[2] - is the result of every member with the searched for given name
            //Table[3] - is the result of every member with the searched for last name

            if (searchType.ToUpper() == "ID")
            {
                addListItems(ds.Tables[1].Rows);
            }
            else if (searchType.ToUpper() == "NAME")
            {
                addListItems(ds.Tables[2].Rows);

                addListItems(ds.Tables[3].Rows);
            }

        }

        private void addListItems(DataRowCollection drColl)
        {
            foreach (DataRow row in drColl)
            {
                //create a new member object and fill in the ID, FirstName, and LastName
                Member m = new Member();
                //set each property of m with the results from the dataset (ds)
                m.ID = int.Parse(row.ItemArray.GetValue(0).ToString());
                m.GivenName = row.ItemArray.GetValue(2).ToString(); //GetValue(1) is the member's Title i.e. Mr, Mrs, Ms etc
                m.LastName = row.ItemArray.GetValue(3).ToString();

                ListViewItem newList = new ListViewItem(m.ID.ToString());
                newList.SubItems.Add(m.GivenName.ToString());
                newList.SubItems.Add(m.LastName.ToString());
                lsvSelectMember.Items.Add(newList);

            }
        }

        
    }
}
